#include <QAction>
#include <QFileDialog>
#include <QtDebug>
//#include <QMetaType>
#include "mainwindow_02.h"
#include "ui_mainwindow_02.h"

#include "process_files/ui_operate_manager_02.h"
#include "data_files/track_data_table_model_02.h"
#include "data_files/track_data_02.h"
#include "data_files/constant_variable.h"

MainWindow_02::MainWindow_02(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow_02)
    , manager_(new Ui_operate_manager_02(this))
    , model_(new Track_data_table_model_02())
{
    ui->setupUi(this);

    init_widgets();
    init_connections();

    setAttribute(Qt::WA_DeleteOnClose);

    //子线程处理model，大数据时出现程序异常退出，目前还没有好的处理方法
//    qRegisterMetaType<QList<QPersistentModelIndex> >("QList<QPersistentModelIndex>");
//    model_->moveToThread(&model_thread_);
//    model_thread_.start();
}

MainWindow_02::~MainWindow_02()
{
    if ((nullptr != model_)
            && (thread() == model_->thread())) {
        delete model_;
        model_ = nullptr;
    }

    model_thread_.quit();
    model_thread_.wait();

    delete ui;
}

void MainWindow_02::init_widgets()
{
    open_file_action_ = new QAction(tr("打开文件"), this);
    split_data_action_ = new QAction(tr("拆分数据"), this);
    exit_action_ = new QAction(tr("退出"), this);

    file_menu_ = menuBar()->addMenu(tr("File"));
    file_menu_->addAction(open_file_action_);
    file_menu_->addAction(split_data_action_);
    file_menu_->addSeparator();
    file_menu_->addAction(exit_action_);

    // Set Model/View
    ui->tableView->setModel(model_);
}

void MainWindow_02::init_connections()
{
    connect(open_file_action_, &QAction::triggered,
            this, &MainWindow_02::sl_open_files);

    connect(split_data_action_, &QAction::triggered,
            this, &MainWindow_02::sl_split_files_content);

    connect(this, &MainWindow_02::si_split_files_content,
            manager_, &Ui_operate_manager_02::sl_split_file_content);

    connect(exit_action_, &QAction::triggered,
            manager_, &Ui_operate_manager_02::sl_exit_application);

    connect(ui->btn_start_filter_, &QPushButton::clicked,
            this, &MainWindow_02::sl_filter_conditions);

    connect(this, &MainWindow_02::si_open_files,
            manager_, &Ui_operate_manager_02::sl_open_files);

    connect(manager_, &Ui_operate_manager_02::si_send_track_to_model,
            this, &MainWindow_02::si_send_track_to_model);

    connect(this, &MainWindow_02::si_send_track_to_model,
            model_, &Track_data_table_model_02::sl_add_track_data);

//    connect(manager_, &Ui_operate_manager_02::si_updte_model_by_filter,
//            model_, &Track_data_table_model_02::sl_update_model_by_filter);

//    connect(model_, &Track_data_table_model_02::si_hide_row,
//            ui->tableView, &QTableView::hideRow);

//    connect(model_, &Track_data_table_model_02::si_show_row,
//            ui->tableView, &QTableView::showRow);

    connect(manager_, &Ui_operate_manager_02::si_updte_model_by_filter,
            model_, &Track_data_table_model_02::sl_add_filter_track_data);

//    connect(ui->tableView, &QTableView::setRowHidden,
//            this, &MainWindow_02::sl_test01);
}

void MainWindow_02::sl_open_files()
{
    QStringList file_name_list = QFileDialog::getOpenFileNames();

    if ( !file_name_list.isEmpty()) {
        if ( model_->has_flight_track()) {
            model_->clear_track_data();
        }

        emit si_open_files(file_name_list);
    }
}

void MainWindow_02::sl_split_files_content()
{
    QStringList file_name_list = QFileDialog::getOpenFileNames();

    if ( !file_name_list.isEmpty()) {
        emit si_split_files_content(file_name_list);
    }
}

void MainWindow_02::sl_filter_conditions()
{
    QMap<QString, QVariant>* filter(new QMap<QString, QVariant>());

    if ( !ui->edit_call_sign_->text().isEmpty()) {
        filter->insert(CALL_SIGN, ui->edit_call_sign_->text().trimmed());
    }

    if ( !ui->edit_ssr_->text().isEmpty()) {
        filter->insert(SSR_CODE, ui->edit_ssr_->text().trimmed());
    }

    if ( !ui->spin_max_altitude_->text().isEmpty()) {
        filter->insert(MAX_LATATIUDE, ui->spin_max_altitude_->text().trimmed());
    }

    if ( !ui->spin_min_altitude_->text().isEmpty()) {
        filter->insert(MIN_LATATIUDE, ui->spin_min_altitude_->text().trimmed());
    }

    model_->clear_track_data();
    manager_->get_filter_conditions(filter, model_->get_backup_track_data());
}

void MainWindow_02::sl_hide_row(int row)
{
    ui->tableView->hideRow(row);
}

void MainWindow_02::sl_test(int row, bool flag)
{
    qDebug()<< row << flag;
}

void MainWindow_02::sl_test01(int row, bool flag)
{
    Q_UNUSED(row)
    Q_UNUSED(flag)

//    qDebug()<< row << flag << "==================" << endl;
}

void MainWindow_02::sl_set_row_hidden(int row, bool flag)
{
//    ui->tableView->setRowHidden(row, flag);
    if ( flag) {
        ui->tableView->showRow(row);
    } else {
        ui->tableView->hideRow(row);
    }
}
